package com.pwc.conversionTool.dataAdaptor;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

public class CsvUtil extends BaseUtil {

	private char columnSeparator;

	private char lineSeparator;

	private CSVReader reader;

	private List<String[]> allData;

	
	public CsvUtil(byte[] csvFile) throws IOException {
		super();
		reader = new CSVReader(new InputStreamReader(new ByteArrayInputStream(
				csvFile)));
		allData = reader.readAll();
		initialContent();
		initialHeader();
	}

	public CsvUtil(byte[] csvFile, char columnSeparator, char lineSeparator)
			throws IOException {
		super();
		this.columnSeparator = columnSeparator;
		this.lineSeparator = lineSeparator;
		reader = new CSVReader(new InputStreamReader(new ByteArrayInputStream(
				csvFile)), lineSeparator, columnSeparator);
		allData = reader.readAll();
		initialContent();
		initialHeader();
	}

	public CsvUtil(InputStream in, char columnSeparator, char lineSeparator)
			throws IOException {
		super();
		this.columnSeparator = columnSeparator;
		this.lineSeparator = lineSeparator;
		reader = new CSVReader(new InputStreamReader(in), lineSeparator,
				columnSeparator);
		allData = reader.readAll();
		initialContent();
		initialHeader();
	}

	public CsvUtil(InputStream in) throws IOException {
		super();
		reader = new CSVReader(new InputStreamReader(in));
		allData = reader.readAll();
		initialContent();
		initialHeader();
	}

	public char getColumnSeparator() {
		return columnSeparator;
	}

	public void setColumnSeparator(char columnSeparator) {
		this.columnSeparator = columnSeparator;
	}

	public char getLineSeparator() {
		return lineSeparator;
	}

	public void setLineSeparator(char lineSeparator) {
		this.lineSeparator = lineSeparator;
	}

	@Override
	public List<String[]> getAllData() {
		List<String[]> all= new ArrayList<String[]>();
		
		all.add(header);
		
		all.addAll(content);
		
		return all;
	}

	public void initialHeader() {
		if (allData != null && allData.size() != 0) {
			this.header = allData.get(0);
		} else {
			this.header = null;
		}
		
		renameHeaderSameName();
	}
	
	

	@Override
	public String[] getHeader() {
		return this.header;
	}

	public void initialContent() {
		if (allData != null && allData.size() > 1) {
			List<String[]> result = new ArrayList<String[]>(allData);
			result.remove(0);
			content = result;
		} else {
			content = null;
		}
	}

	@Override
	public List<String[]> getContent() {
		return this.content;
	}

	@Override
	public int getColumnIndex(String ColumnName) {

		if (allData != null && allData.size() != 0) {
			for (int i = 0; i < allData.get(0).length; i++) {
				if (allData.get(0)[i].equals(ColumnName))
					return i;
			}
		}

		return -1;
	}

	@Override
	public void setContent(List<String[]> content) {
		this.content = content;
	}

	// @Override
	// public void setContent(List<String[]> content) {
	// if (allData != null && allData.size() > 1) {
	// for (int i = 1; i < allData.size(); i++) {
	// allData.remove(i);
	// }
	// if (content != null && content.size() > 1)
	// for (int j = 0; j < content.size(); j++) {
	// allData.add(content.get(j));
	// }
	// }
	// }

}
